package com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationbuilding;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationbuilding.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationbuilding.AccommodationBuildingDO;
import com.nanhai.buddhist.college.module.collegebase.convert.accommodationbuilding.AccommodationBuildingConvert;
import com.nanhai.buddhist.college.module.collegebase.service.accommodationbuilding.AccommodationBuildingService;

@Api(tags = "管理后台 - 楼栋管理")
@RestController
@RequestMapping("/collegebase/accommodation-building")
@Validated
public class AccommodationBuildingController {

    @Resource
    private AccommodationBuildingService accommodationBuildingService;

    @PostMapping("/create")
    @ApiOperation("创建楼栋管理")
    public CommonResult<String> createAccommodationBuilding(@Valid @RequestBody AccommodationBuildingCreateReqVO createReqVO) {
        return success(accommodationBuildingService.createAccommodationBuilding(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新楼栋管理")
    public CommonResult<Boolean> updateAccommodationBuilding(@Valid @RequestBody AccommodationBuildingUpdateReqVO updateReqVO) {
        accommodationBuildingService.updateAccommodationBuilding(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除楼栋管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    public CommonResult<Boolean> deleteAccommodationBuilding(@RequestParam("id") String id) {
        accommodationBuildingService.deleteAccommodationBuilding(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得楼栋管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<AccommodationBuildingRespVO> getAccommodationBuilding(@RequestParam("id") String id) {
        AccommodationBuildingDO accommodationBuilding = accommodationBuildingService.getAccommodationBuilding(id);
        return success(AccommodationBuildingConvert.INSTANCE.convert(accommodationBuilding));
    }

    @GetMapping("/list")
    @ApiOperation("获得楼栋管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<AccommodationBuildingRespVO>> getAccommodationBuildingList(@RequestParam("ids") Collection<String> ids) {
        List<AccommodationBuildingDO> list = accommodationBuildingService.getAccommodationBuildingList(ids);
        return success(AccommodationBuildingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得楼栋管理分页")
    public CommonResult<PageResult<AccommodationBuildingRespVO>> getAccommodationBuildingPage(@Valid AccommodationBuildingPageReqVO pageVO) {
        PageResult<AccommodationBuildingDO> pageResult = accommodationBuildingService.getAccommodationBuildingPage(pageVO);
        return success(AccommodationBuildingConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出楼栋管理 Excel")
    @OperateLog(type = EXPORT)
    public void exportAccommodationBuildingExcel(@Valid AccommodationBuildingExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AccommodationBuildingDO> list = accommodationBuildingService.getAccommodationBuildingList(exportReqVO);
        // 导出 Excel
        List<AccommodationBuildingExcelVO> datas = AccommodationBuildingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "楼栋管理.xls", "数据", AccommodationBuildingExcelVO.class, datas);
    }

}
